package com.tv.enoguia.fragment;

import java.util.ArrayList;

import com.tv.enoguia.DetailBussinessActivity;
import com.tv.enoguia.R;
import com.tv.enoguia.adapter.ListBussinessAdapter;
import com.tv.enoguia.adapter.ListFavoritesAdapter;
import com.tv.enoguia.bean.Bussiness;
import com.tv.enoguia.bean.Categories;
import com.tv.enoguia.database.MySQLiteHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class FavourScr extends Fragment {

	TextView title;
	ListBussinessAdapter adapter;
	ArrayList<Bussiness> bussinesses = new ArrayList<Bussiness>();
	ListView listFavor;
	MySQLiteHelper db;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		db = new MySQLiteHelper(getActivity());
	}
	
	
	
	ViewGroup v;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if(v==null) {
			v = (ViewGroup) inflater.inflate(R.layout.scr_favour, container, false);
		} else {
			((ViewGroup) v.getParent()).removeView(v);
		}
		
		title = (TextView) v.findViewById(R.id.title);
		listFavor = (ListView) v.findViewById(R.id.list_favour);
		addData();
		adapter = new ListBussinessAdapter(getActivity(), bussinesses);
		listFavor.setAdapter(adapter);
		init();
		
		listFavor.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				/*Intent t = new Intent(getActivity(), DetailBussinessActivity.class);
				Log.i("DATA", "id gui di: "+bussinesses.get(position).getId());
				t.putExtra("id", bussinesses.get(position).getId());
				t.putExtra("liked", 1);
				startActivity(t);*/
				
				Fragment productDetailFragment = DetailBussinessFragment.newInstance();
				FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
				transaction.addToBackStack(null);
				Bundle b = new Bundle();
				b.putInt("liked", 1);
				b.putString("id", bussinesses.get(position).getId());
				productDetailFragment.setArguments(b);
				transaction.replace(R.id.replace, productDetailFragment).commit();
			}
			
		});
		return v;
	}




	void addData(){
		bussinesses.clear();
		bussinesses = db.getBussinessLike();
	}
	
	void init(){
		title.setText(getString(R.string.txt_favour));
	}


	@Override
	public void onResume() {
		super.onResume();
		Log.i("STATE", "RESUME");
		addData();
		adapter = new ListBussinessAdapter(getActivity(), bussinesses);
		listFavor.setAdapter(adapter);
	}


	@Override
	public void onPause() {
		super.onPause();
		Log.i("STATE", "PAUSE");
	}
	
	
	
}
